package com.ittest.server.impl;

import com.ittest.server.SendMailServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;


@Service //复杂一些的邮件
public class SendMailServerImpl2 implements SendMailServer {

    @Autowired
    private JavaMailSender javaMailSender;


    //发送人
    private String form = "17331988610@163.com";
    //接收人
    private String to = "1021854489@qq.com";
    //标题
    private String subject = "测试邮件高级+图片";
    //正文
    private String context = "<img src='https://img1.baidu.com/it/u=1993115086,135022393&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800'/><a href='https://www.itcast.cn'>点开有惊喜</a>";
    @Override
    public void sendmail() {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(form+"(杨闯杰)");//加上后面的字符串，这样在接收者邮箱就会替代显示为发件人信息
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context,true);
            //添加附件
            File f1 = new File("/Users/admin/Desktop/我的电脑/D盘/javaDemo/spring-boot/springboot/springboot_23_mail/src/main/resources/idea隐藏不想看到的文件.png");
            File f2 = new File("/Users/admin/Desktop/我的电脑/D盘/javaDemo/spring-boot/springboot/springboot_23_mail/target/springboot_23_mail-0.0.1-SNAPSHOT.jar");
            helper.addAttachment("杨幂.png",f1);
            helper.addAttachment(f2.getName(),f2);

            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
